package com.lingnan.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lingnan.pojo.Active;
import com.lingnan.pojo.ActiveHistory;
import com.lingnan.pojo.History;
import com.lingnan.service.HistoryService;
import com.lingnan.util.Result;
import com.lingnan.util.ResultGenerator;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName HistoryController
 * @Description TODO
 * @Author kang
 * @Version 1.0
 */
@RestController
@CrossOrigin
@RequestMapping("/history")
public class HistoryController {

    @Autowired
    HistoryService historyService;

    @ApiOperation("添加浏览记录")
    @PostMapping("/add")
    public Result addHistory(@RequestBody @ApiParam("传学号no，活动activeId，时间time")History history){
        if (ObjectUtils.isNull(history)){
            return ResultGenerator.genErrorResult(201,"参数错误");
        }
        boolean save = historyService.save(history);
        if (save){
            return ResultGenerator.genSuccessResult(200, "添加历史记录成功");
        }else {
            return ResultGenerator.genErrorResult(201,"参数错误");
        }
    }

    @ApiOperation("获得某个人的所有浏览记录")
    @PostMapping("/getAll")
    public Result getAllHistory(@RequestParam @ApiParam("传学号过来") String no){
        if (StringUtils.isBlank(no)){
            return ResultGenerator.genErrorResult(201,"参数错误");
        }
        List<ActiveHistory> byNo = historyService.getByNo(no);
        return ResultGenerator.genSuccessResult(200, byNo, "历史记录展示成功");
    }

    @ApiOperation("删除某条浏览记录")
    @DeleteMapping("/delete")
    public Result deleteHistory(@RequestParam @ApiParam("传id") Integer id){
        if (ObjectUtils.isNull(id)){
            return ResultGenerator.genErrorResult(201,"参数错误");
        }
        LambdaQueryWrapper<History> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(History::getId, id);
        boolean remove = historyService.remove(lambdaQueryWrapper);
        if (remove){
            return ResultGenerator.genSuccessResult(200,"移除成功");
        }else {
            return ResultGenerator.genErrorResult(201, "移除失败");
        }
    }
}
